<?php

namespace app\admin\controller\csmwj;

use think\Db;
use Exception;
use PDOException;
use think\exception\ValidateException;
use addons\csmwj\library\xapp\csmwj\utils\XpPaperUtils;
use addons\csmwj\library\xcore\xcore\base\XcABackend;
use addons\csmwj\library\xcore\xcore\utils\XcConfigUtils;
use addons\csmwj\library\xcore\xcore\utils\XcRequestUtils;
use addons\csmwj\library\xcore\xcore\utils\XcAdminTabUtils;

/**
 * 工单
 *
 * @icon fa fa-circle-o
 */
class Paper extends XcABackend
{

    /**
     * Paper模型对象
     * @var \app\admin\model\csmwj\Paper
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\csmwj\Paper;
        $this->view->assign("localesList", $this->model->getLocalesList());
        $this->view->assign("cfghasdatelimitList", $this->model->getCfghasdatelimitList());
        $this->view->assign("cfghastimelimitList", $this->model->getCfghastimelimitList());
        $this->view->assign("cfgdisplayscoreList", $this->model->getCfgdisplayscoreList());
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("themebannertypeList", $this->model->getThemebannertypeList());
        $this->view->assign("themetitleshowList", $this->model->getThemetitleshowList());
        $this->view->assign("themequestionnoshowList", $this->model->getThemequestionnoshowList());

        // csmwj:设置页面tabs
        $xc_tabid = XcRequestUtils::param("ids");
        XcAdminTabUtils::append_control_initialize('paper', $xc_tabid, __CLASS__, $this->view);

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function add()
    {
        if (false === $this->request->isPost()) {
            return $this->view->fetch();
        }
        $params = $this->request->post('row/a');
        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $params = $this->preExcludeFields($params);

        if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
            $params[$this->dataLimitField] = $this->auth->id;
        }
        $result = false;
        Db::startTrans();
        try {
            //是否采用模型验证
            if ($this->modelValidate) {
                $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
                $this->model->validateFailException()->validate($validate);
            }

            //csmwj
            $params['status'] = 'draft';
            $params['cfgresultmsg'] = '您已完成本次工单，感谢您的帮助与支持';
            XpPaperUtils::setDefaultTheme($params);

            $result = $this->model->allowField(true)->save($params);
            Db::commit();
        } catch (ValidateException | PDOException | Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result === false) {
            $this->error(__('No rows were inserted'));
        }
        $this->success();
    }

    public function port($ids){
        $id = $ids;

        $addons = XcConfigUtils::xpconfig("addons_code");
        $this->view->assign("addons",urlencode($addons));
        $this->view->assign("h5_url",XcConfigUtils::config("xcbaseurl")."/assets/addons/{$addons}/h5/#/pages/csmwj/paper?id={$id}");
        $this->view->assign("page",urlencode("pages/csmwj/paper"));
        $this->view->assign("scene",urlencode("id={$id}"));


        return $this->view->fetch();
    }


}

